package com.wsc.wisdomchinaqinghe.Tools;

import android.content.Context;
import android.content.res.AssetManager;

import com.wsc.wisdomchinaqinghe.javaBean.address.CityModel;
import com.wsc.wisdomchinaqinghe.javaBean.address.DistrictModel;
import com.wsc.wisdomchinaqinghe.javaBean.address.ProvinceModel;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class AddressParser {

	private Context mContext;

	/**
	 * 所有省
	 */
	public String[] mProvinceDatas;
	/**
	 * key - 省 value - 市
	 */
	public Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
	/**
	 * key - 市 values - 区
	 */
	public Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();

	public List<String> mProvinceNames = new ArrayList<>();

	public AddressParser(Context context) {
		this.mContext = context;
		initProvinceDatas();
	}

	/**
	 * 解析省市区的XML数据
	 */

	public void initProvinceDatas() {
		List<ProvinceModel> provinceList = null;
		AssetManager asset = mContext.getAssets();
		try {
			InputStream input = asset.open("province_data.xml");
			// 创建一个解析xml的工厂对象
			SAXParserFactory spf = SAXParserFactory.newInstance();
			// 解析xml
			SAXParser parser = spf.newSAXParser();
			XmlParserHandler handler = new XmlParserHandler();
			parser.parse(input, handler);
			input.close();
			// 获取解析出来的数据
			provinceList = handler.getDataList();
			// */ 初始化默认选中的省、市、区
			if (provinceList != null && !provinceList.isEmpty()) {
				List<CityModel> cityList = provinceList.get(0).getCityList();
				if (cityList != null && !cityList.isEmpty()) {
					List<DistrictModel> districtList = cityList.get(0).getDistrictList();
				}
			}
			// */
			mProvinceDatas = new String[provinceList.size()];
			for (int i = 0; i < provinceList.size(); i++) {
				// 遍历所有省的数据
				mProvinceDatas[i] = provinceList.get(i).getName();
				mProvinceNames.add(provinceList.get(i).getName());
				
				List<CityModel> cityList = provinceList.get(i).getCityList();
				String[] cityNames = new String[cityList.size()];
				for (int j = 0; j < cityList.size(); j++) {
					// 遍历省下面的所有市的数据
					cityNames[j] = cityList.get(j).getName();
					List<DistrictModel> districtList = cityList.get(j).getDistrictList();
					String[] distrinctNameArray = new String[districtList.size()];
					DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];
					for (int k = 0; k < districtList.size(); k++) {
						// 遍历市下面所有区/县的数据
						DistrictModel districtModel = new DistrictModel(districtList.get(k).getName());
						distrinctArray[k] = districtModel;
						distrinctNameArray[k] = districtModel.getName();
					}
					// 市-区/县的数据，保存到mDistrictDatasMap
					mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
				}
				// 省-市的数据，保存到mCitisDatasMap
				mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {

		}
	}

	@SuppressWarnings({ "unused", "rawtypes" })
	public List<String> showCountrys(String citysName) {
		initProvinceDatas();
		List<String> countrys = new ArrayList();
		// 获取key的集合
		Set<String> keySet = mDistrictDatasMap.keySet();
		// 遍历key集合，获取value
		for (String key : keySet) {
			if (citysName.equals(key)) {
				String[] value = mDistrictDatasMap.get(key);
				for (int i = 0; i < value.length; i++) {
					countrys.add(value[i]);
				}
			}
		}
		return countrys;
	}
	
	@SuppressWarnings({ "unused", "rawtypes" })
	public List<String> showCitys(String provinceName) {
		initProvinceDatas();
		List<String> citys = new ArrayList();
		// 获取key的集合
		Set<String> keySet = mCitisDatasMap.keySet();
		// 遍历key集合，获取value
		for (String key : keySet) {
			if (provinceName.equals(key)) {
				String[] value = mCitisDatasMap.get(key);
				for (int i = 0; i < value.length; i++) {
					citys.add(value[i]);
				}
			}
		}
		return citys;
	}

	public List<String> showProvinces() {
		List<String> provinces = new ArrayList();
		// 获取key的集合
		Set<String> keySet = mCitisDatasMap.keySet();
		// 遍历key集合，获取value
		for (String key : keySet) {
			provinces.add(key);
		}
		return provinces;
	}
}
